package com.yzd.learnjava.containers;

import com.yzd.learnjava.util.Print;

public class AssociateArray<K, V> {
	private Object[][] pairs ;
	private int index ;
	
	public AssociateArray(int length) {
		pairs = new Object[length][2];
	}
	
	public void put(K key, V value) {
		if(index >= pairs.length) {
			throw new ArrayIndexOutOfBoundsException();
		}
		pairs[index++] = new Object[] {key, value};
	}
	
	public V get(K key) {
		for(int i = 0 ; i < index; i++) {
			if(pairs[i][0].equals(key)) {
				return (V)pairs[i][1];
			}
		}
		return null;
	}
	
	public String toString() {
		StringBuilder b = new StringBuilder();
		for(int i = 0 ; i < index ; i++) {
			b.append(pairs[i][0].toString());
			b.append(" : ");
			b.append(pairs[i][1].toString());
			if(i < index -1) {
				b.append("\n");
			}
		}
		return b.toString();
	}
	
	public static void main(String[] args) {
		AssociateArray<String, String> map = new AssociateArray<String, String>(6);
		map.put("sky", "blue");
		map.put("grass", "green");
		map.put("ocen", "dancing");
		map.put("tree", "tall");
		map.put("sun", "warm");
		
		try {
			map.put("extra", "object");
		} catch (Exception e) {
			Print.print("too many");
		}
		
		Print.print(map);
		Print.print(map.get("ocen"));
	}
}













